<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专利名称" prop="proName">
        <el-input v-model="queryParams.proName" placeholder="请输入专利名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分组状态" prop="proAuthor">
        <el-select v-model="queryParams.proGroupStatus" placeholder="请选择分组状态">
          <el-option label="全部专利" value=""></el-option>
          <el-option label="未分组" value="0"></el-option>
          <el-option label="已分组" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button style="float:right" type="danger" size="mini" icon="el-icon-upload" plain @click="distribute">分配到该组
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="adoptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="专利编号" align="center" prop="id" width="90" />
      <el-table-column label="专利名称" align="center" prop="proName" />
      <el-table-column label="专利文件" align="center" prop="proFile" width="200">
        <template slot-scope="scope">
          <a @click="downloadFile(scope.row)" class="fileStyle">{{downName(scope.row.proFile)}}</a>
          <el-progress :percentage="progress1" :format="format" v-if="scope.row.id==rowid?true:false">
          </el-progress>
        </template>
      </el-table-column>
      <!-- <el-table-column label="专利类型" align="center" prop="proType" /> -->
      <el-table-column label="专利描述" align="center" prop="proDescr" />
      <el-table-column label="上传人" align="center" prop="proAuthor" />
      <el-table-column label="上传时间" align="center" prop="proCreatetime" width="200PX" />
      <el-table-column label="项分组状态" align="center" prop="proGroupStatus">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.proGroupStatus==0">未分组</el-tag>
          <el-tag type="danger" v-else>已分组</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==0">待审核</el-tag>
          <el-tag type="success" v-else-if="scope.row.status==1">已通过</el-tag>
          <el-tag type="danger" v-else="scope.row.status==2">被驳回</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listProjectData } from "@/api/system/project/projectData";
import download from "@/plugins/download"
import { addProjects } from "@/api/system/group/group";
export default {
  name: "ProjectData",
  data () {
    return {
      activeName: 'second',
      // 遮罩层
      loading: true,
      statusText: "",
      // 选中数组
      ids: [],
      proIds: [],
      proGroupStatus: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专利数据表格数据
      projectDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      rowid: '',
      progress1: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: null,
        proFile: null,
        proType: null,
        proDescr: null,
        proAuthor: null,
        proCreatetime: null,
        proGroupStatus: null,
        groupId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      examineList: [],
      adoptList: [],
      reject: []
    };
  },
  created () {
    this.getList();
  },
  computed: {
    progress () {
      return this.$store.state.myProject.progress
    }
  },
  watch: {
    progress (newval, oldval) {
      this.progress1 = newval
    }
  },
  methods: {
    format (percentage) {
      return percentage === 100 ? '完成' : `${percentage}%`;
    },
    downloadFile (data) {
      if (this.progress1 != 0) {
        this.$message({
          message: '已经有一个专利正在下载',
          type: 'warning'
        });
      } else {
        this.rowid = data.id
        download.resource(data.proFile)
        setTimeout(() => {
          this.rowid = ""
          this.progress1 = 0;
        }, 3000)

      }

    },
    downName (file) {
      let data = file.split("/");
      return data[data.length - 1];
    },
    distribute () {
      this.loading = true
      console.log(this.proGroupStatus)
      for (var i = 0; i < this.proGroupStatus.length; i++) {
        if (this.proGroupStatus[i] == 1) {
          this.$message.error("请选择未分组的专利")
          this.loading = false
          return 0;
        }
      }
      if (this.proIds == "" || this.proIds == null) {
        this.$message.error("请选择未分组的专利")
        this.loading = false
      } else {
        const data = {
          id: this.$route.query.id,
          proIds: this.proIds,
        };
        addProjects(data).then((response) => {
          this.$modal.msgSuccess("导入成功");
          this.getList()
          this.$router.push('/group/expert_examine')
        });
      }
      this.loading = false

    },
    /** 查询专利数据列表 */
    getList () {
      this.loading = true;
      this.examineList = []
      this.adoptList = []
      this.reject = []
      listProjectData(this.queryParams).then(response => {
        this.projectDataList = response.rows;
        this.total = response.total;
        this.loading = false;
        for (var i = 0; i < this.projectDataList.length; i++) {
          if (this.projectDataList[i].status == 0) {
            this.examineList.push(this.projectDataList[i])
          } else if (this.projectDataList[i].status == 1) {
            this.adoptList.push(this.projectDataList[i])
          } else {
            this.reject.push(this.projectDataList[i])
          }
        }
        this.total = this.adoptList.length
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        proName: null,
        proFile: null,
        proType: null,
        proDescr: null,
        proAuthor: null,
        proCreatetime: null,
        proGroupStatus: 0,
        groupId: null,
        userId: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.proIds = selection.map(item => item.id)
      this.proGroupStatus = selection.map(item => item.proGroupStatus)
      console.log(this.proGroupStatus)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

  }
};
</script>
<style scoped>
.fileStyle {
  padding: 12px;
  color: #442ed0;
}
.fileStyle:hover {
  text-decoration: underline !important;
  cursor: pointer;
  color: #2300ff;
}
</style>